﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuanLyVanBanBUS;

public partial class QLVB_User_SoanThaoMauBieu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            layDanhSachNoiPhatHanh();
            if (cmbMaNoiPhatHanh.Items.Count > 0)
            {
                cmbMaNoiPhatHanh.SelectedIndex = 0;

                layThongTinChiTietMaNoiPhatHanh();
            }
        }
    }

    protected void layDanhSachNoiPhatHanh()
    {
        NoiPhatHanhBUS nphBUS = new NoiPhatHanhBUS();

        cmbMaNoiPhatHanh.DataSource = nphBUS.layDanhSachNoiPhatHanh();
        cmbMaNoiPhatHanh.DataTextField = "TenNoiPhatHanh";
        cmbMaNoiPhatHanh.DataValueField = "MaNoiPhatHanh";
        cmbMaNoiPhatHanh.DataBind();
    }
    private void layThongTinChiTietMaNoiPhatHanh()
    {
        NoiPhatHanh nphDTO = new NoiPhatHanh();
        nphDTO.MaNoiPhatHanh = cmbMaNoiPhatHanh.SelectedValue;

        NoiPhatHanhBUS nphBUS = new NoiPhatHanhBUS();

        IList<NoiPhatHanh> lstThongTinNoiPhatHanh = nphBUS.layThongTinNoiPhatHanhTheoMaNoiPhatHanh(nphDTO);
        switch (nphDTO.MaNoiPhatHanh)
        {
            case "BIDV":
                txtTenDonViSoanThao.Text = "BIDV";
                break;
            case "BIDVCM":
                txtTenDonViSoanThao.Text = "BIDV CAMAU";
                break;
            case "PT":
                txtTenDonViSoanThao.Text = "BIDV CAMAU";
                break;
            default:
                txtTenDonViSoanThao.Text = string.Empty;
                break;
        }
    }
    protected void btnDongY_Click(object sender, EventArgs e)
    {
        //Kiem tra
        if (txtNoiDungMauBieu.Text == string.Empty)
        {
            Alert.Show("Noi dung mau bieu khong duoc rong");

            return;
        }

        if (fuplTenFileMauBieu.PostedFile.FileName == string.Empty)
        {
            Alert.Show("File mau bieu dinh kem khong duoc rong");

            return;
        }

        if (txtTenDonViSoanThao.Text == string.Empty)
        {
            Alert.Show("Ten don vi soan thao khong duoc rong");

            return;
        }

        MauBieu mbDTO = new MauBieu();
        mbDTO.NoiDungMauBieu = txtNoiDungMauBieu.Text;
        mbDTO.TenFileMauBieu = fuplTenFileMauBieu.PostedFile.FileName;
        mbDTO.TenFileCongVan = fuplTenFileCongVan.PostedFile.FileName;
        mbDTO.MaNoiPhatHanh = cmbMaNoiPhatHanh.SelectedValue.ToString();
        mbDTO.MaNguoiSoanThao = (String)Session["MaNguoiDung"];
        mbDTO.NgaySoanThao = DateTime.Now;
        mbDTO.TenDonViSoanThao = txtTenDonViSoanThao.Text;

        MauBieuBUS mbBUS = new MauBieuBUS();
        mbBUS.themMauBieu(mbDTO);

        uploadFileMauBieu();
        uploadFileCongVan();

        Alert.Show("Da them thanh cong");
    }
    protected void btnDong_Click(object sender, EventArgs e)
    {
        Session.Clear();

        Response.Redirect("~/pHome.aspx");
    }
    protected void uploadFileMauBieu()
    {
        string tenFile = fuplTenFileMauBieu.PostedFile.FileName;

        string path = Server.MapPath("..//VanBanLuuTru/MauBieu");

        path += "/" + tenFile;

        fuplTenFileMauBieu.PostedFile.SaveAs(path);
    }
    protected void uploadFileCongVan()
    {
        string tenFile = fuplTenFileCongVan.PostedFile.FileName;

        string path = Server.MapPath("..//VanBanLuuTru/MauBieu");

        path += "/" + tenFile;

        fuplTenFileMauBieu.PostedFile.SaveAs(path);
    }
    protected void cmbMaNoiPhatHanh_SelectedIndexChanged(object sender, EventArgs e)
    {
        layThongTinChiTietMaNoiPhatHanh();
    }
}